﻿using System.Web.Mvc;
using BookTheBook.Data;
using BookTheBook.Data.Model;
using BookTheBook.Web.Helpers;

namespace BookTheBook.Web.Controllers
{
    public class BookController : Controller
    {
        private readonly ILibraryRepository _bookRepository;

        public BookController(ILibraryRepository bookRepository)
        {
            //TODO: doesn't work now - reconfigure Spring.NET
            _bookRepository = bookRepository;
        }

        //
        // GET: /Book/

        public ActionResult Index(int? page)
        {
            const int pageSize = 10; //move to config later on

            var allBooks = _bookRepository.GetAllBooks();
            var booksForPagination = new PaginatedListCollection<Book>(allBooks, page ?? 0, pageSize);
            return View(booksForPagination);
        }

        //
        // GET: /Book/Details/5

        public ActionResult Details(int id)
        {
            return View();
        }

        //
        // GET: /Book/Create

        public ActionResult Create()
        {
            return View();
        } 

        //
        // POST: /Book/Create

        [HttpPost]
        public ActionResult Create(FormCollection collection)
        {
            try
            {
                // TODO: Add insert logic here

                return RedirectToAction("Index");
            }
            catch
            {
                return View();
            }
        }

        //
        // GET: /Book/Edit/5

        public ActionResult Edit(int id)
        {
            return View();
        }

        //
        // POST: /Book/Edit/5

        [HttpPost]
        public ActionResult Edit(int id, FormCollection collection)
        {
            try
            {
                // TODO: Add update logic here
 
                return RedirectToAction("Index");
            }
            catch
            {
                return View();
            }
        }
    }
}
